#!/bin/sh
#
# Copyright © 2014 Cask Data, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#

set -e

if [ -h /usr/bin/file-tailer ]; then
    rm -f /usr/bin/file-tailer
fi

if [ -h /etc/file-tailer/conf ]; then
    rm -f /etc/file-tailer/conf
fi

if [ ! -d /etc/file-tailer ]; then
    mkdir -p /etc/file-tailer
fi

if [ ! -d /var/log/file-tailer ]; then
    mkdir -p /var/log/file-tailer
fi

chown -R file-tailer:file-tailer /var/log/file-tailer
chmod 755 /var/log/file-tailer

ln -s /usr/lib/file-tailer/bin/file-tailer /usr/bin/file-tailer
ln -s /usr/lib/file-tailer/conf /etc/file-tailer/conf

if [ -f /usr/lib/file-tailer/bin/file-tailer.default ]; then
    mv /usr/lib/file-tailer/bin/file-tailer.default /etc/default/file-tailer
fi

if [ -f /usr/lib/file-tailer/bin/file-tailer.init.d ]; then
    mv /usr/lib/file-tailer/bin/file-tailer.init.d /etc/init.d/file-tailer
fi

if [ -x "/etc/init.d/file-tailer" ]; then
	if [ ! -e "/etc/init/file-tailer.conf" ]; then
		if ! chkconfig_loc="$(type -p "chkconfig")" || [ -z "$chkconfig_loc" ]; then
		    update-rc.d -f file-tailer defaults >/dev/null
		else
		    chkconfig --add file-tailer >/dev/null
		fi
	fi
fi


